#include "stdafx.h"

#include "parser_errors.h"
#include "parser_state.h"

namespace funpl
{

namespace parser
{

// Emits a silent parse error.
LEAN_NOLTINLINE void silent_error(parser_state &state)
{
	state.tree->valid = false;
	throw parse_exception();
}

// Skips the current source of errors (to be called from catch).
LEAN_NOLTINLINE void skip_error(parser_state &state)
{
	if (state.pErrors)
	{
		state.accepted = true;
		state.silent = true;
	}
	else
		throw;
}

// Emits a parse error.
LEAN_NOLTINLINE void error(parser_state &state, debug::error_code::t code, const char *pMsg)
{
	state.tree->valid = false;

	if (state.pErrors)
	{
		size_t line, pos;
		line_and_pos(line, pos, state);

		state.pErrors->errors.push_back(
				debug::error(code, (pMsg) ? pMsg : debug::error_name(code), line, pos)
			);
	}
	
	throw parse_exception();
}

// Emits a parse warning.
LEAN_NOLTINLINE void warning(parser_state &state, debug::error_code::t code, const char *pMsg)
{
	if (state.pErrors)
	{
		size_t line, pos;
		line_and_pos(line, pos, state);

		state.pErrors->warnings.push_back(
				debug::error(code, (pMsg) ? pMsg : debug::error_name(code), line, pos)
			);
	}
}

} // namespace

} // namespace